package com.qishon.pc.domain.Controller;

import com.qishon.pc.domain.Application.DownloadZipManager;
import com.qishon.pc.domain.Application.PreUploadManager;
import com.qishon.pc.domain.Application.ProgressControlManager;
import com.qishon.pc.domain.model.ProgressControl;
import com.qishon.pc.domain.model.ProgressSteps;
import com.qishon.pc.domain.service.ProgressControlService;
import com.qishon.pc.domain.service.ProgressStepsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by xm on 2016/12/23.
 */
@Controller

public class ProgressControlFileController {
    static int k = 1; // 定义递归次数变量

    @Value("${run.path}")
    private String runPath;

    private static final Logger logger = LoggerFactory.getLogger(ProgressControlFileController.class);
    @Autowired
    ProgressControlManager progressControlManager;
    @Autowired
    ProgressControlService progressControlService;
    @Autowired
    ProgressStepsService progressStepsSerivce;
    @Autowired
    DownloadZipManager downloadZipManager;
    @Autowired
    PreUploadManager preUploadManager;
    @RequestMapping("/pcFileUpload")
    public String pcFileUpload(Map<String,Object> map,  Model model) {
            model.addAttribute("progressControls","pcFileUpload");
        return "pcFileUpload";
    }

    @GetMapping("/pcFileDownload")
    public String pcFileDownload(Map<String,Object> map,  Model model) {
        model.addAttribute("progressControls", "pcFileDownload");
        return "pcFileDownload";
    }




    @RequestMapping("/Message")
    @ResponseBody
    public String Message(@RequestParam(value = "filename", required = false) String filename,@RequestParam(value = "file", required = false) MultipartFile file,@RequestParam(value = "file1", required = false) MultipartFile file1,@RequestParam(value = "file2", required = false) MultipartFile file2)
            throws Exception {
        return preUploadManager.PreMessage(filename, file, file1, file2);
    }


    @RequestMapping("/saveFileToServer")
    @ResponseBody
    public String saveFileToServer(@RequestParam(value = "filename", required = false) String filename,@RequestParam(value = "file", required = false) MultipartFile file,@RequestParam(value = "file1", required = false) MultipartFile file1,@RequestParam(value = "file2", required = false) MultipartFile file2)
            throws Exception {
        return preUploadManager.PresaveFileToServer(filename, file, file1, file2);
    }


//    @RequestMapping("/getChunkedFileSize")
//    public int getChunkedFileSize(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        return preUploadManager.PregetChunkedFileSize(request,response);
//    }
//
//
//    @RequestMapping("/appendUpload2Server")
//    public int appendUpload2Server(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        return preUploadManager.PreappendUpload2Server(request,response);
//    }

    /**
     * 获取已上传的文件大小
     * @param request
     * @param response
     */
    @RequestMapping("/getChunkedFileSize")
    public void getChunkedFileSize(HttpServletRequest request, HttpServletResponse response) throws Exception {
        preUploadManager.PregetChunkedFileSize((org.apache.catalina.servlet4preview.http.HttpServletRequest) request,response);
    }
    /**
     *
     * 断点文件上传 1.先判断断点文件是否存在 2.存在直接流上传 3.不存在直接新创建一个文件 4.上传完成以后设置文件名称
     *
     */
    @RequestMapping("/appendUpload2Server")
    public void appendUpload2Server(HttpServletRequest request, HttpServletResponse response) throws Exception {
        preUploadManager.PreappendUpload2Server((org.apache.catalina.servlet4preview.http.HttpServletRequest) request,response);
    }





    @RequestMapping("/downloadzip")
    @ResponseBody
    public  HttpServletResponse downLoadFiles(HttpServletRequest request, HttpServletResponse response) {
        String modelName = request.getParameter("MODEL");
        Map<String,Object> map = new HashMap<>();
        map.put("modelName",modelName);
        //String stepNo = request.getParameter("04.Compress");
        ProgressControl progressControl = null;
        List<ProgressControl> progressControls = progressControlService.findByGirard(map);
        for (int i = 0; i < progressControls.size();i++){

            if(!progressControls.get(i).getProgress().equals("100%") &&
                    !progressControls.get(i).getProgress().equals("0%")){
               progressControl = progressControls.get(i);
            }
        }
        ProgressSteps progressSteps = null;
        if (progressControl != null){
            List<ProgressSteps> progressStepsList = progressStepsSerivce.findByGirardId(progressControl);
            for (int k = 0;k < progressStepsList.size();k++){
                if(progressStepsList.get(k).getStepNo().toUpperCase().equals("05.COMPRESS")){
                    progressSteps = progressStepsList.get(k);
                }
            }
        }
        if (progressSteps == null || progressSteps.getAllFiles() != progressSteps.getCompletedFiles()){
            //return response;
        }
        try {
            //需要压缩的文件
            File file = new File(runPath+"\\"+"06.Upload\\"+modelName);
            if(!file.exists()){
                file.mkdirs();
            }
            //压缩包存放路径
            String out=runPath+"\\"+"06.Upload\\"+modelName+".zip";
            downloadZipManager.zip(out,file,"");
            return downloadZipManager.downloadZip(out,response);
        }catch (Exception e) {
            e.printStackTrace();
        }

        return response ;
    }
}